<template>
  <div>
    <el-dialog
      class="custom-dialog"
      v-model="dialogVisible"
      :title="title"
      width="500px"
      @close="handleClose"
    >
      <el-form
        class="data-form"
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input
            class="data-input"
            v-model="form.roleName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            class="data-input"
            v-model="form.remark"
            placeholder="请输入"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, watch, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { roleAdd, roleEdit } from '@/api/manager/systemManage/role.js'
defineOptions({
  name: 'AddDialog',
})
const props = defineProps({
  detail: {
    type: Object,
  },
})
watch(
  () => props.detail,
  () => {
    form.value = { ...props.detail }
  },
)
const emit = defineEmits(['close'])
const title = ref('')
const dialogVisible = ref(false)
const handleClose = () => {
  dialogVisible.value = false
  emit('close')
}
const formRef = ref(null)
const form = ref({})
const rules = ref({
  roleName: [{ required: true, message: '请输入图层名称', trigger: 'blur' }],
})
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      const params = {
        roleName: form.value.roleName,
        remark: form.value.remark,
      }
      let res
      if (form.value.pkid) {
        res = await roleEdit(form.value.pkid, params)
      } else {
        res = await roleAdd(params)
      }
      if (res.success) {
        form.value = {}
        ElMessage.success('保存成功')
        dialogVisible.value = false
        emit('close', true)
      } else {
        ElMessage.error(res.message)
      }
    } else {
      ElMessage.warning('请正确录入信息')
    }
  })
}
defineExpose({ dialogVisible, title })
onMounted(() => {})
</script>
<style lang="scss" scoped></style>
